package lib

//连接Dynamodb

import (
	"log"

	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/dynamodb"
)

//连接参数
const DynamoDBAwsAccessKey = "AKIA4SBKFCJAXUOUXUN2"
const DynamoDBAwsSecretKey = "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"
const DynamoDBAwsRegion = "us-west-2"

var DynamoClient *dynamodb.DynamoDB

func init() {
	var err error
	if DynamoClient, err = NewClient(DynamoDBAwsAccessKey, DynamoDBAwsSecretKey, DynamoDBAwsRegion); err != nil {
		panic(err)
	}
	log.Printf("dynamoDB 连接成功...")
}

//真正执行连接
func NewClient(aws_access_key, aws_secret_key, aws_region string) (svc *dynamodb.DynamoDB, err error) {
	aws_config := &aws.Config{
		Credentials: credentials.NewStaticCredentials(aws_access_key, aws_secret_key, ""),
		Region:      aws.String(aws_region),
	}
	sess, err := session.NewSession()
	if err != nil {
		return
	}
	svc = dynamodb.New(sess, aws_config)
	return
}
